package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import myz.date.gui.CalendarPad;
import myz.date.gui.NumberLenghtLimitedDmt;
import db.domain.Student;
import db.sqldao.StudentSqlDao;

/**
 * 充值，挂失界面
 * 
 * @author 陈千琪
 * @date 2011-06-29
 * @version 1.0.0.0
 * 
 */
@SuppressWarnings("serial")
public class AddMoney extends JDialog implements MouseListener, ActionListener {
	private JLabel lblMoney;// 余额
	private JTextField txtMoney;
	private JLabel lblSetMoney;// 充值
	private JTextField txtSetMoney;
	private JPanel jp;
	private JLabel lblLose;
	private JRadioButton rdbLose[];// 挂失

	private JButton btnAdd;// 添加
	private JButton btnLose;// 挂失
	private JLabel lblPicture;// 系统图片
	Student Student = new Student();

	public AddMoney(Student student) {
		// 添加一些组件，添加到jp上
		int width = 600, height = 450;
		int x0 = 15, y0 = 50, w0 = 65, h0 = 20;
		int x1 = 100, y1 = 50;
		int w1 = 120, ww = 30, hh = 100;

		this.setTitle("校园卡充值界面");
		this.setLayout(null);
		jp = new JPanel(null);// 使用坐标布局

		TitledBorder tb = new TitledBorder("校园卡余额信息");// 标题
		tb.setTitleColor(Color.red);// 标题颜色
		// 标题字体
		tb.setTitleFont(new Font("黑体", Font.BOLD, 20));
		// 边框颜色和宽度
		tb.setBorder(new LineBorder(Color.blue, 2, false));// false直角true 圆角
		jp.setBorder(tb);
		jp.setBounds(0, 0, width, height);
		this.add(jp);

		Student = student;
		// 第一行
		lblMoney = new JLabel("余额：");
		lblMoney.setHorizontalAlignment(JLabel.RIGHT);// 对齐方式
		// lblNo.setBorder(new LineBorder(Color.red, 1, false));
		lblMoney.setBounds(x0, y0, w0, h0);
		jp.add(lblMoney);
		txtMoney = new JTextField();
		txtMoney.setBounds(x0 + x1, y0, w1, h0);
		txtMoney.setBorder(new LineBorder(Color.black, 1, true));
		txtMoney.setDocument(new NumberLenghtLimitedDmt(5));
		txtMoney.setEnabled(false);// 文本框不可编辑
		jp.add(txtMoney);

		// 第二行
		lblSetMoney = new JLabel("充值：");
		lblSetMoney.setHorizontalAlignment(JLabel.RIGHT);// 对齐方式
		// lblNo.setBorder(new LineBorder(Color.red, 1, false));
		lblSetMoney.setBounds(x0, y0 + y1, w0, h0);
		jp.add(lblSetMoney);

		txtSetMoney = new JTextField();
		txtSetMoney.setBounds(x0 + x1, y0 + y1, w1, h0);
		txtSetMoney.setBorder(new LineBorder(Color.black, 1, true));
		txtSetMoney.setDocument(new NumberLenghtLimitedDmt(5));
		txtSetMoney.setText("0");
		jp.add(txtSetMoney);

		// 第三行
		lblLose = new JLabel("挂失：");
		lblLose.setHorizontalAlignment(JLabel.RIGHT);
		// lblLose.setBorder(new LineBorder(Color.red, 1, false));
		lblLose.setBounds(x0, 20 + h0 + hh, w0, h0);
		jp.add(lblLose);

		String[] strSex = { "是", "否" };
		rdbLose = new JRadioButton[strSex.length];
		ButtonGroup bg = new ButtonGroup();// 按钮组（和单选有关）
		for (int i = 0; i < strSex.length; i++) {
			rdbLose[i] = new JRadioButton(strSex[i]);
			rdbLose[i].setBorder(new LineBorder(Color.red, 1, false));
			bg.add(rdbLose[i]);
			jp.add(rdbLose[i]);
			rdbLose[i].setBounds(lblLose.getX() + w0 + 45 * i + 35, lblLose
					.getY(), 45, h0);
		}
		rdbLose[0].setSelected(true);// 被选择

		// 第四行
		btnAdd = new JButton("充值");
		btnAdd.setBounds(130, 350, 80, 30);
		btnAdd.addActionListener(this);

		jp.add(btnAdd);

		btnLose = new JButton("挂失");
		btnLose.setBounds(btnAdd.getX() + btnAdd.getWidth() + 50,
				btnAdd.getY(), btnAdd.getWidth(), btnAdd.getHeight());
		btnLose.addActionListener(this);
		jp.add(btnLose);

		this.setModal(true);// 窗口独占模式（JDialog独有）
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		// 匿名内部类
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				closeWindow();
			}
		});

		displayStudent(student);

		// 设置图片的位置
		lblPicture = new JLabel("<html>　　系<br>　　统<br>　　图<br>　　片<br></html>");
		// lblPicture.setBorder(new LineBorder(Color.blue, 5, true));
		lblPicture.setBounds(0, 0, width, height);// 设置图片位置
		displayPictur displayPictur = new displayPictur(4, lblPicture);
		jp.add(lblPicture);

		this.setSize(width, height);
		this.setResizable(false);
		this.setVisible(true);
	}

	protected void closeWindow() {
		// 关闭窗口
		this.dispose();// 释放本窗口资源
	}

	public void mouseClicked(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON1/* 左键 */) {
			// 获取鼠标的位置
			double x = e.getLocationOnScreen().getX();
			double y = e.getLocationOnScreen().getY();
			CalendarPad cp = new CalendarPad(2010, 4, 27, x, y);

		}
	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {

	}

	public void mousePressed(MouseEvent e) {

	}

	public void mouseReleased(MouseEvent e) {

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAdd) {
			add();
		} else if (e.getSource() == btnLose) { // 返回
			lose();
		}
	}

	/**
	 * 挂失
	 */

	private void lose() {
		String lose = null; // 丢失
		for (int i = 0; i < rdbLose.length; i++) {
			if (rdbLose[i].isSelected()) {
				lose = rdbLose[i].getText();
				break;
			}
		}
		Student.setLose(lose);
		StudentSqlDao sDao = new StudentSqlDao();
		boolean flag = sDao.updateStudent(Student);
		if (flag) {
			JOptionPane.showMessageDialog(this, "恭喜你【挂失】 成功！");
		} else {
			JOptionPane.showMessageDialog(this, "对不起【挂失】 失败！");
		}
	}

	/**
	 * 充值
	 */
	private void add() {

		Student.setMoney(Integer.parseInt(txtSetMoney.getText()));
		StudentSqlDao sDao = new StudentSqlDao();
		boolean flag = sDao.updateStudent(Student);
		if (flag) {
			JOptionPane.showMessageDialog(this, "恭喜你【充值】 成功！");
		} else {
			JOptionPane.showMessageDialog(this, "对不起【充值】 失败！");
		}
	}

	/**
	 * 将余额，挂失信息显示到窗口的组件上
	 * 
	 * @param student
	 */
	private void displayStudent(Student student) {
		txtMoney.setText(Integer.toString(student.getMoney()));// 余额
		String lose = student.getLose();// 丢失
		for (int i = 0; i < rdbLose.length; i++) {
			if (lose.equals(rdbLose[i].getText())) {
				rdbLose[i].setSelected(true);
				break;
			}
		}
	}

}
